{{ define "main" }}
  <div id="content">
    <div id="sidewrap">
      <div id="sidenav">
        {{ partial "sidenav.html" (dict "sidenavType" .Params.sidenav "data" .Site.Data "language" $.Site.Language.Lang ) }}
      </div>
    </div>
    <div id="contentwrap">
      <h1>{{ .Title }}</h1>
      {{- .Content -}}

      {{ $usergroupsPath := (printf "data/%s/usergroups/usergroups.toml" $.Language.Lang) }}

      {{ $usergroupsData := "" }}
      {{ if (fileExists $usergroupsPath) -}}
        {{ $data := index .Site.Data $.Language.Lang }}
        {{ $usergroupsData = $data.usergroups }}
      {{ else }}
        {{ $dataDefaultLanguage := index .Site.Data .Sites.First.Language.Lang }}
        {{ $usergroupsData = $dataDefaultLanguage.usergroups }}
      {{ end }}

      <ul>
        <li>
          <a href="#europe">{{ i18n "europe" }}</a> ( {{ len $usergroupsData.usergroups.Europe }} user groups)
        </li>
        <li>
          <a href="#north-america">{{ i18n "northAmerica" }}</a> ( {{ len $usergroupsData.usergroups.NorthAmerica }} user groups)
        </li>
        <li>
          <a href="#asia">{{ i18n "asia" }}</a> ( {{ len $usergroupsData.usergroups.Asia }} user groups)
        </li>
        <li>
          <a href="#south-america">{{ i18n "southAmerica" }}</a> ( {{ len $usergroupsData.usergroups.SouthAmerica }} user groups)
        </li>
        <li>
          <a href="#oceania">{{ i18n "oceania" }}</a> ( {{ len $usergroupsData.usergroups.Oceania }} user groups)
        </li>
        <li>
          <a href="#global">{{ i18n "global" }}</a> ( {{ len $usergroupsData.usergroups.Global }} user groups)
        </li>
      </ul>
      <br />

      <!-- Load usergroups and number of them-->
      {{ $usergroupsCodes := slice }}
      {{ $usergroupsCodesTimes := newScratch }}

      {{ with $usergroupsData.usergroups }}
        {{ range . }}
          {{ range . }}
            {{ if .countryCode }}
              {{ $usergroupsCodes = $usergroupsCodes | append .countryCode }}

              {{ if $usergroupsCodesTimes.Get .countryCode }}
                {{ $usergroupsCodesTimes.Add .countryCode 1 }}
              {{ else }}
                {{ $usergroupsCodesTimes.Set .countryCode 1 }}
              {{ end }}
            {{ end }}
          {{ end }}
        {{ end }}
      {{ end }}

      {{ $usergroupsCodes = $usergroupsCodes | uniq }}

      {{ $usergroupsTimes := slice }}
      {{ range $element := $usergroupsCodes }}
        {{ $usergroupsTimes = $usergroupsTimes | append ($usergroupsCodesTimes.Get .) }}
      {{ end }}

      {{ $usergroupsImageUrl := (printf "https://chart.googleapis.com/chart?cht=t&chs=400x200&chtm=world&chco=ffffff,ffbe38,600000&chf=bg,s,4D89F9&chd=t:%s&chld=%s" (delimit $usergroupsTimes ",") (delimit $usergroupsCodes "")) }}

      <img alt="Map with the usergroups" src={{ $usergroupsImageUrl }} />

      <h3 id="europe">{{ i18n "europe" }}</h3>
      {{ with $usergroupsData.usergroups }}
        <dl>
        {{ range (sort .Europe "name" "asc") }}
          <dt>
            <a href="{{ .url }}">{{ .name }}</a>
          </dt>
          <dd>
            <p id="{{ .id }}">{{ .description | safeHTML }}</p>
          </dd>
        {{ end }}
        </dl>
      {{ end }}

      <h3 id="north-america">{{ i18n "northAmerica" }}</h3>
      {{ with $usergroupsData.usergroups }}
        <dl>
        {{ range (sort .NorthAmerica "name" "asc") }}
          <dt>
            <a href="{{ .url }}">{{ .name }}</a>
          </dt>
          <dd>
            <p id="{{ .id }}">{{ .description | safeHTML }}</p>
          </dd>
        {{ end }}
        </dl>
      {{ end }}

      <h3 id="asia">{{ i18n "asia" }}</h3>
      {{ with $usergroupsData.usergroups }}
        <dl>
        {{ range (sort .Asia "name" "asc") }}
          <dt>
            <a href="{{ .url }}">{{ .name }}</a>
          </dt>
          <dd>
            <p id="{{ .id }}">{{ .description | safeHTML }}</p>
          </dd>
        {{ end }}
        </dl>
      {{ end }}

      <h3 id="oceania">{{ i18n "oceania" }}</h3>
      {{ with $usergroupsData.usergroups }}
        <dl>
        {{ range (sort .Oceania "name" "asc") }}
          <dt>
            <a href="{{ .url }}">{{ .name }}</a>
          </dt>
          <dd>
            <p id="{{ .id }}">{{ .description | safeHTML }}</p>
          </dd>
        {{ end }}
        </dl>
      {{ end }}

      <h3 id="south-america">{{ i18n "southAmerica" }}</h3>
      {{ with $usergroupsData.usergroups }}
        <dl>
        {{ range (sort .SouthAmerica "name" "asc") }}
          <dt>
            <a href="{{ .url }}">{{ .name }}</a>
          </dt>
          <dd>
            <p id="{{ .id }}">{{ .description | safeHTML }}</p>
          </dd>
        {{ end }}
        </dl>
      {{ end }}

      <h3 id="global">{{ i18n "global" }}</h3>
      {{ with $usergroupsData.usergroups }}
        <dl>
        {{ range (sort .Global "name" "asc") }}
          <dt>
            <a href="{{ .url }}">{{ .name }}</a>
          </dt>
          <dd>
            <p id="{{ .id }}">{{ .description | safeHTML }}</p>
          </dd>
        {{ end }}
        </dl>
      {{ end }}
      {{ if .GitInfo }}
      <hr />
      <div class="last-modified">
        <p><strong>{{ i18n "last-modified" }}</strong>: {{ .GitInfo.AuthorDate | time.Format ":date_long" }} {{ i18n "by" }} <a href="https://cgit.freebsd.org/doc/commit/?id={{ .GitInfo.AbbreviatedHash }}" target="_blank">{{ .GitInfo.AuthorName }}</a></p>
      </div>
      {{ end }}
    </div>
  </div>
{{ end }}
